package com.qzsoft.system.modules.permission.rolebusiness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qzsoft.jeemis.common.service.BaseService;
import com.qzsoft.jeemis.common.utils.ConvertUtils;
import com.qzsoft.system.modules.permission.dept.dao.SysDeptMapper;
import com.qzsoft.system.modules.permission.rolebusiness.dao.SysRoleBusinessMapper;
import com.qzsoft.system.modules.permission.rolebusiness.dao.SysUserRoleBusinessMapper;
import com.qzsoft.system.modules.permission.rolebusiness.dto.SysUserRoleBusinessVo;
import com.qzsoft.system.modules.permission.rolebusiness.entity.SysUserRoleBusinessEntity;
import com.qzsoft.system.modules.permission.rolebusiness.service.SysUserRoleBusinessService;

import com.qzsoft.system.repository.sys.entity.SysDeptEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("SysUserRoleBusinessService")
public class SysUserRoleBusinessServiceImpl extends BaseService implements SysUserRoleBusinessService {

    @Autowired
    private SysRoleBusinessMapper sysRoleBusinessMapper;
    @Autowired
    private SysUserRoleBusinessMapper sysUserRoleBusinessMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Override
    public List<SysUserRoleBusinessVo> queryUserRoleBusinessListByUserId(Long id) {
        List<SysUserRoleBusinessVo> list = sysRoleBusinessMapper.queryUserRoleBusinessListByUserId(id);
        return list;
    }

    @Override
    @Transactional
    public void saveOrUpdate(Long id, List<SysUserRoleBusinessVo> userRoleBusinessList) {
        //李化军修改 ========用户和审批角色的关系修改为先删除再新增
        QueryWrapper<SysUserRoleBusinessEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        int delete = sysUserRoleBusinessMapper.delete(wrapper);
        for (SysUserRoleBusinessVo sysUserRoleBusinessVo : userRoleBusinessList) {
            SysUserRoleBusinessEntity sysUserRoleBusinessEntity = ConvertUtils.sourceToTarget(sysUserRoleBusinessVo, SysUserRoleBusinessEntity.class);
            sysUserRoleBusinessEntity.setUserId(String.valueOf(id));
            QueryWrapper<SysDeptEntity> sysDeptEntityQueryWrapper = new QueryWrapper<>();
            sysDeptEntityQueryWrapper.eq("id", sysUserRoleBusinessVo.getDeptId());
            SysDeptEntity sysDeptEntity = sysDeptMapper.selectOne(sysDeptEntityQueryWrapper);
            if (sysDeptEntity!=null){
                sysUserRoleBusinessEntity.setDeptPkid(sysDeptEntity.getPkid());
                sysUserRoleBusinessEntity.setDeptName(sysDeptEntity.getName());
                sysUserRoleBusinessMapper.insert(sysUserRoleBusinessEntity);
            }

        }



//        for (SysUserRoleBusinessVo roleBusinessVO : userRoleBusinessList) {
//            roleBusinessVO.setDeptPkid(roleBusinessVO.getPkid());
//            //遍历每个接收来的对象
//            SysUserRoleBusinessEntity sysUserRoleBusinessEntity = ConvertUtils.sourceToTarget(roleBusinessVO, SysUserRoleBusinessEntity.class);
//            sysUserRoleBusinessEntity.setUserId(String.valueOf(id));
//            //设置为删除标志为未删除
//            sysUserRoleBusinessEntity.setHasDelete(0);
//
//            int i = 0;
//            //将传入的每个接收对象与已经存在的进行比较  break说明有该记录已经存在并进行更新操作
//            // 遍历时  接收对象中有 库中也有的情况
//            for (SysUserRoleBusinessEntity entity : existList) {
//                //userid+deptid+rolebusinessid 都一样才算一条相同记录
//                if (sysUserRoleBusinessEntity.getUserId().equals(entity.getUserId()) && roleBusinessVO.getRoleBusinessId().equals(entity.getRoleBusinessId()) && roleBusinessVO.getDeptId().equals(entity.getDeptId())) {
//                    //存在相同的  库中 1  更新为0
//                    entity.setHasDelete(0);
//                    sysUserRoleBusinessMapper.updateById(entity);
//                    //i++说明是已经存在的不需要插入
//                    i++;
//                    break;
//                }
//            }
//            //说明是新记录进行插入操作
//            if (i == 0) {
//                sysUserRoleBusinessMapper.insert(sysUserRoleBusinessEntity);
//            }
//        }
//
//        //修改时 如果传入的对象list中没有，数据库中却存在的，进行更新has_delete操作
//        for (SysUserRoleBusinessEntity entity : existList) {
//            int i = 0;
//            for (SysUserRoleBusinessVo roleBusinessVO : userRoleBusinessList) {
//                //如果库中已经存在
//                System.out.println("用户id的比较结果为========" + id.toString().equals(entity.getUserId().toString()));
//                System.out.println("用户角色的比较结果为========" + roleBusinessVO.getRoleBusinessId().equals(entity.getRoleBusinessId()));
//                System.out.println("用户部门的比较结果为=========" + roleBusinessVO.getDeptId().equals(entity.getDeptId()));
//                if (id.toString().equals(entity.getUserId().toString()) && roleBusinessVO.getRoleBusinessId().equals(entity.getRoleBusinessId()) && roleBusinessVO.getDeptId().equals(entity.getDeptId())) {
//                    i++;
//                    break;
//                }
//            }
//            //新的数据中不存在  库中这条记录
//            if (i == 0) {
//                entity.setHasDelete(1);
//                sysUserRoleBusinessMapper.updateById(entity);
//            }
//        }
    }

    @Override
    public void updateHasDelete(Long[] ids) {

        sysUserRoleBusinessMapper.updateHasFlag(ids);
    }

    private void deleteByUserIds(Long[] userIds) {

        QueryWrapper<SysUserRoleBusinessEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);
        sysUserRoleBusinessMapper.delete(queryWrapper);
    }
}
